package com.zmj.framework.authorization.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.Header;
import com.zmj.amc.sdk.AmcClientHolder;
import com.zmj.amc.sdk.entity.User;
import com.zmj.framework.authorization.client.AuthorizationClient;
import com.zmj.framework.authorization.filter.AuthorizationConfig;
import com.zmj.framework.authorization.vo.AccessToken;
import com.zmj.framework.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 李飞
 * @Date: 2020/11/30 8:56
 */
@Api(tags = "统一鉴权")
@Slf4j
@RestController
@RequestMapping("/auth")
@AllArgsConstructor
public class AuthorizationController {

    private AuthorizationConfig authorizationConfig;

    private final static String GRANT_TYPE_GET_TOKEN = "authorization_code";

    private final static String GRANT_TYPE_REFRESH_TOKEN = "refresh_token";

    private final static String TOKEN_TYPE_HINT = "access_token";

    @Autowired
    private AuthorizationClient authorizationClient;

    @ApiOperation(value = "获取token", notes = "获取token")
    @GetMapping(value = "/getToken")
    public AccessToken getToken(@RequestParam String code, @RequestParam String redirectUri) {
        log.info("获取token参数======>>code:" + code +"redirectUri:" + redirectUri);
        return authorizationClient.token(GRANT_TYPE_GET_TOKEN,authorizationConfig.getClientId(),authorizationConfig.getClientSecret(),code,redirectUri);
    }

    @ApiOperation(value = "刷新token", notes = "刷新token")
    @GetMapping(value = "/refreshToken")
    public AccessToken refreshToken(@RequestParam String refreshToken) {
        log.info("刷新token参数======>>refreshToken:" + refreshToken);
        return authorizationClient.refreshToken(GRANT_TYPE_REFRESH_TOKEN,authorizationConfig.getClientId(),authorizationConfig.getClientSecret(),refreshToken);
    }

    @ApiOperation(value = "登出", notes = "登出")
    @GetMapping(value = "/logout")
    public Result logout(HttpServletRequest request) {
        String authorization = ServletUtil.getHeader(request, Header.AUTHORIZATION.toString(), "utf-8");

        if (StrUtil.isNotBlank(authorization)) {
            String token = authorization.substring("Bearer ".length());
            Result restResult= authorizationClient.logout(token,TOKEN_TYPE_HINT,authorizationConfig.getClientId(),authorizationConfig.getClientSecret());
            if (restResult.getCode() == 1000) {
                return Result.success();
            } else {
                return Result.fail();
            }
        }
        return Result.fail();
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping(value = "/userInfo")
    public Result<User> userInfo(HttpServletRequest request) {
        User user = null;
        String authorization = ServletUtil.getHeader(request, Header.AUTHORIZATION.toString(), "utf-8");
        if (StrUtil.isNotBlank(authorization)) {
            String token = authorization.substring("Bearer ".length());
            user = AmcClientHolder.getUserByToken(token);
        }
        return Result.success(user);
    }

}
